package com.lq.nine;

import java.util.Arrays;

public class QuickSort1 {

    public static void main(String[] args) {
        int[] arr = {3, 1, 5, 4, 2};
        quickSort(arr, 0, arr.length -1);
        Arrays.stream(arr).forEach(System.out::println);
    }

    public static void quickSort(int[] arr, int left, int right) {
        if (left >= right) return;
        int l = left;
        int r = right;
        int key = arr[l];
        while (l < r) {
            while (l < r && arr[r] >= key) r--;
            if (l < r) arr[l++] = arr[r];
            while (l < r && arr[l] <= key) l++;
            if (l < r) arr[r--] = arr[l];
        }
        arr[l] = key;
        quickSort(arr, left, l - 1);
        quickSort(arr, l + 1, r);
    }
}
